package com.aaa.ni.service;

import com.aaa.common.entity.Intergral;
import com.aaa.common.entity.News;
import com.aaa.common.entity.NewsNewReading;
import com.aaa.common.entity.Users;
import com.aaa.common.util.Constants;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.DateUtil;
import com.aaa.common.util.Result;
import com.aaa.common.vo.NewsVo;
import com.aaa.ni.dao.IntergralMapper;
import com.aaa.ni.dao.NewsMapper;
import com.aaa.ni.dao.NewsNewReadingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.util.unit.DataUnit;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * fileName:NewsServiceImpl
 * description:
 *
 * @author:wzl createTime:2020/9/3 15:20
 */
@Service
@Transactional
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private IUserInfoService iUserInfoService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private NewsNewReadingMapper newsNewReadingMapper;
    @Autowired
    private IntergralMapper intergralMapper;

    private Users user;

    @Override
    public Result pageByParam(int pageNo, int pageSize, Map map) {
        //自定义异常 如果pageNo和pageSize没有值 直接抛出自定义异常
        map.put("start",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        List<News> newsList = newsMapper.pageByParam(map);
        int total = newsMapper.pageByParamCount(map);
        Map resultMap = new HashMap();
        resultMap.put("newList",newsList);
        resultMap.put("total",total);
        return new Result(Constants.RESULT.SUCCESS,Constants.RESULT.SUCMESSAGE,resultMap);
    }


    @Override
    public int deleteByPrimaryKey(Integer id) {
        return newsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(NewsVo newsVo, String accessToken) {
        //获取登录后的用户信息  调用接口
       //Users user = iUserInfoService.getByToken(accessToken);
        //获取登录后的用户信息  直接从缓存中拿
        user = (Users) redisTemplate.opsForValue().get(accessToken);
        News news =  new News();
        //VO 转 BO
        news.setSummary(newsVo.getSummary());
        news.setIsTop(newsVo.getIsTop());
        news.setTitle(newsVo.getTitle());
        news.setTitleThick(newsVo.getTitleThick());
        news.setTitleColor(newsVo.getTitleColor());
        news.setBigPicPath(newsVo.getBigPicPath());
        news.setSmallPicPath(newsVo.getSmallPicPath());
        news.setTypeId(newsVo.getTypeId());
        news.setSecurityLevel(newsVo.getSecurityLevel());
        news.setContent(newsVo.getContent());
        //设置新闻中固定字段   userName 用户名 一般唯一识别  和userId一样都是唯一的  realName 员工姓名  nickName 昵称
        news.setAddGroupid(user.getGroupid());
        news.setAddRoomid(user.getDeptid());
        news.setAddUsername(user.getUsername());
        news.setAddtime(new Date());

        return newsMapper.insertSelective(news);
    }

    @Override
    public News selectByPrimaryKey(Integer id, String accessToken,Integer isBack) {
        //自定义异常处理 id不能为空
        if (id==null) {
            throw new CustomException(Constants.CustomException.ID_NOT_NULL.getErrorCode(),
                    Constants.CustomException.ID_NOT_NULL.getErrorMessage());
        }
        //自定义异常处理 Token不能为空
        if (accessToken==null) {
            throw new CustomException(Constants.CustomException.TOKEN_NOT_NULL.getErrorCode(),
                    Constants.CustomException.TOKEN_NOT_NULL.getErrorMessage());
        }
        user = (Users) redisTemplate.opsForValue().get(accessToken);
        if (isBack!=null){
            //isBack为空时是前台人员增加  不为空时是后台人员增加
            //点击查看新闻时 向最新阅读记录表添加
            NewsNewReading newsNewReading = new NewsNewReading();
            newsNewReading.setAddtime(new Date());
            newsNewReading.setUsername(user.getUsername());
            newsNewReading.setNewsId(id);
            newsNewReading.setRoomid(user.getDeptid());
            newsNewReading.setName(user.getUsername());
            newsNewReadingMapper.insertSelective(newsNewReading);
            //添加积分
            Map mapParam = new HashMap();
            mapParam.put("userName",user.getUsername());
            mapParam.put("integralType",Constants.InfoMarketIntegralType.READNEWSTYPE);
            //使用工具时间类把当前时间转换为
            mapParam.put("addTime", DateUtil.dateToString(new Date()));
            mapParam.put("newsId",id);
            List<Intergral> intergralList = intergralMapper.selectByParam(mapParam);
            //判读当前用户今天是否点击同一条新闻
           if (intergralList==null||intergralList.isEmpty()){
                Intergral intergral = new Intergral();
                intergral.setAddtime(new Date());
                //使用事先规定好的常量类
                intergral.setIntegralType(Constants.InfoMarketIntegralType.READNEWSTYPE);
                intergral.setName(user.getUsername());
                intergral.setNewsId(id);
                intergral.setRoomid(user.getDeptid());
                intergral.setScore(1);
                intergral.setUsername(user.getUsername());
                intergralMapper.insertSelective(intergral);
            }

        }
        return newsMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(News record) {
        if (StringUtils.isEmpty(record.getTitle())||StringUtils.isEmpty(record.getContent())){
            throw new CustomException(Constants.CustomException.INVALID_ARGUMENT.getErrorCode(),
                    Constants.CustomException.INVALID_ARGUMENT.getErrorMessage());
        }
        return newsMapper.updateByPrimaryKeySelective(record);
    }
}
